package com.my.network.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author: marshal
 * @date: 2023-03-21 16:00
 */
public class MySocketServer {
    public static void main(String[] args) {
        try (
                // 创建一个ServerSocket
                ServerSocket serverSocket = new ServerSocket(8099);
                // 监听来自客户端的连接
                Socket socket = serverSocket.accept();
                // 从socket中获取输入流
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                // 创建输出流
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        ) {
            // 读取客户端的消息
            String receiveMessage = br.readLine();
            System.out.println("收到来自客户端的消息：");
            // 输出message
            System.out.println(receiveMessage);
            // 向客户端写入消息
            String replyMessage = "你好，已经收到你的消息了!\n";
            bw.write(replyMessage);
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
